#include <QFileDialog>
#include "../../datamanager/reg3volumedatanode.h"
#include "dctreecontrol.h"

using namespace PBVP;

DCTreeControl::DCTreeControl(FileExtensionManager* pExtManager, QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);

	m_pFileExtManager = pExtManager;

	ui.rangeMin->setText(QString().setNum(0.0f));
	ui.rangeMax->setText(QString().setNum(1.0f));

	connect(ui.volumeFileName, SIGNAL(textChanged(const QString&)),
		this, SLOT(updateOutputPath(const QString &)));

	connect(ui.volumeFileButton, SIGNAL(pressed()), this, SLOT(changeVolumeFile()));
	connect(ui.outputFileButton, SIGNAL(pressed()), this, SLOT(changeDCTPath()));
	connect(ui.propertyAddButton, SIGNAL(pressed()), this, SLOT(addPropertyFile()));
}

DCTreeControl::~DCTreeControl()
{

}

void DCTreeControl::init(Reg3DVolumeDataNode *pVolNode)
{
	if (pVolNode == NULL) {
		ui.volumeFileName->setText(QString());
		return;
	}
	ui.volumeFileName->setText(pVolNode->getName());
	Reg3DVolumeData *pVolData = (Reg3DVolumeData *)(pVolNode->data());
	ui.rangeMin->setText(QString().setNum(pVolData->getMinValue()));
	ui.rangeMax->setText(QString().setNum(pVolData->getMaxValue()));
}

float DCTreeControl::getRangeMin() const
{
	return ui.rangeMin->text().toFloat();
}

float DCTreeControl::getRangeMax() const
{
	return ui.rangeMax->text().toFloat();
}

int DCTreeControl::numOfLevels() const
{
	return ui.numLevels->value();
}

//////////////////////////////////////////////////////////////////////////
// Slots
//////////////////////////////////////////////////////////////////////////
void DCTreeControl::updateOutputPath(const QString& path)
{
	QString outputPath(path);
	ui.DCTFileName->setText(outputPath.append(".dct"));
}

void DCTreeControl::changeVolumeFile()
{
	QString fname = QFileDialog::getOpenFileName(this, "Open Volume File", "",
		m_pFileExtManager->filter(VOLUME_DATA));
	if (fname.isNull()) return;
	ui.volumeFileName->setText(fname);
}

void DCTreeControl::changeDCTPath()
{
	QString fname = QFileDialog::getSaveFileName(this, "Save DCT File", "",
		"DCT Files(*.dct);;All Files(*.*)");
	if (!fname.isNull())
	{
		ui.DCTFileName->setText(fname);
	}
}
void DCTreeControl::addPropertyFile()
{
	QString fname = QFileDialog::getOpenFileName(this, "Open Property File", "",
		m_pFileExtManager->filter(VOLUME_DATA));
	if (!fname.isNull())
	{
		ui.propertyList->addItem(fname);
	}
}
